<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class AdminMaster extends BaseModel
{
    //定义表名 大师团队表
    protected $name = 'longbing_card_decorate_master_team';


    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 团队列表
     */
    public function masterList($dis,$page = 10,$where=array()){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_user_info b','a.staff_id = b.fans_id')
            ->leftJoin('longbing_card_job c','b.job_id = c.id')
            ->leftJoin('longbing_card_company d','b.company_id = d.id')
            ->leftJoin('longbing_card_user e','e.id = a.staff_id')
            ->where($dis)
            ->where(function ($query) use ($where){
                  $query->whereor($where);
              })
            ->field('a.*,b.name as staff_name,c.name as job_name,d.id as company_id,b.phone,d.name as department,d.pid as top_id,b.avatar,e.nickName,e.avatarUrl,b.email')
            ->group('a.id')
            ->order('a.sort desc , a.id DESC')
            ->paginate($page)
            ->toArray();
        $data['data'] = transImagesOne($data['data'],['avatar']);
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['avatar'] = !empty($v['avatar'])?$v['avatar']:$v['avatarUrl'];
                $v['avatar'] = !empty($v['avatar'])?$v['avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
                $v['staff_name'] = !empty($v['staff_name'])?$v['staff_name']:$v['nickName'];
                if($v['top_id']>0){
                    $v['company_name'] = $this->getTop($v['top_id']);
                }else{
                    $v['company_name'] = $v['department'];
                    $v['department']   = '暂无部门';
                }
            }

        }
        return $data;
    }

    public function masterdelUser($where){
        $data = $this->alias('a')
            ->join('longbing_card_user_info b','b.fans_id = a.staff_id')
            ->join('longbing_card_user c','b.fans_id = c.id')
            ->where($where)
            ->field('a.id')
            ->select()
            ->toArray();
        if($data){
            foreach ($data as $val){
                $this->masterDel(['id'=>$val['id']]);
            }
        }
    }
    /**
     * User: chenniang
     * Date: 2019-10-25 14:34
     * @param $id
     * @return void
     * descption:查找顶级
     */
    public function getTop(&$id){
        $data = Db::name('longbing_card_company')->where(['id'=>$id])->find();
        if(!empty($data['pid'])&&$data['pid']>0){
            $this->getTop($data['pid']);
        }
        return !empty($data['name'])?$data['name']:'';
    }

    /**
     * @param $data
     * @return int|string
     * 添加团队
     */
    public function masterAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑团队
     */
    public function masterUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * @return AdminCate
     * 大师团队详情
     */
    public function masterInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        return !empty($data)?$data->toArray():$data;
    }


    /**
     * User: chenniang
     * Date: 2019-10-24 16:50
     * @param $dis
     * @return bool
     * descption:删除大师团队
     */
    public function masterDel($dis){
        $res = self::where($dis)->delete();
        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-10-25 16:27
     * @param $dis
     * @return array
     * descption:获取已选员工id
     */
    public function getStaffId($dis){
        $data = $this->where($dis)->status(-1)->column('staff_id');
        return $data;
    }





}